package mobi.richaze.llbag.infrastructure.hibernate;

import java.util.List;

import javax.annotation.Resource;

import mobi.richaze.llbag.domain.model.order.LineItem;
import mobi.richaze.llbag.domain.model.product.Category;
import mobi.richaze.llbag.domain.model.product.Inventory;
import mobi.richaze.llbag.domain.model.product.Item;
import mobi.richaze.llbag.domain.model.product.Product;
import mobi.richaze.llbag.domain.model.product.ProductRepository;
import mobi.richaze.llbag.infrastructure.util.HibernateSessionFactory;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository("ProductRepositoryHibernate")
@SuppressWarnings("unchecked")
public class ProductRepositoryHibernate implements ProductRepository {
	private static final Logger log = LoggerFactory
			.getLogger(ProductRepositoryHibernate.class);

	@Resource(name = "HibernateRepository")
	HibernateRepository repository;

	public List<Inventory> getInventory() {
		log.debug("getting Inventory instances");
		List<Inventory> list = null;
		try {
			Criteria criteria = HibernateSessionFactory.getSession()
					.createCriteria(Inventory.class, "inv");
			list = criteria.list();
			log.debug("get Account instances successful, result size: "
					+ list.size());
		} catch (Exception ex) {
			log.error("get failed", ex);
			ex.printStackTrace();
		}

		return list;
	}

	public List<Product> getProductsByCategory(String categoryId) {
		Session session = HibernateSessionFactory.getSessionFactory()
				.getCurrentSession();
		session.beginTransaction();
		Category category = repository.getEntity(Category.class, categoryId);
		List<Product> products = category.getProducts();
		session.getTransaction().commit();
		return products;
	}

	public List<Product> getProductsBySearch(String... keywords) {
		return null;
	}

	public int currentQtyInStock(String itemid) {
		Inventory inventory = repository.getEntity(Inventory.class, "1");
		System.out.println(inventory.getQty());
		return 0;
	}

	public Item getItem(String itemId) {
		return null;
	}

	public List<Item> getItemsByProduct(String productId) {
		return null;
	}

	public void takeStock(LineItem items) {

	}

}
